/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hcbank.bussiness;

import com.hcbank.classes.Account;
import com.hcbank.jpa.controllers.AccountEntityJpaController;
import com.hcbank.jpa.controllers.CustomerEntityJpaController;
import com.hcbank.jpa.controllers.exceptions.IllegalOrphanException;
import com.hcbank.jpa.controllers.exceptions.NonexistentEntityException;
import com.hcbank.jpa.controllers.exceptions.PreexistingEntityException;
import com.hcbank.jpa.controllers.exceptions.RollbackFailureException;
import com.hcbank.jpa.entities.AccountEntity;
import com.hcbank.jpa.entities.helper.AccountHelper;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

/**
 *
 * @author NHAHV
 */
public class AccountController {

    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("HCBankPU");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    AccountEntityJpaController jpaController = new AccountEntityJpaController(null, entityManagerFactory);
    CustomerEntityJpaController customerEntityJpaController = new CustomerEntityJpaController(null, entityManagerFactory);
    AccountHelper accountHelper = new AccountHelper();


    public AccountController() {
    }

    public Account ATMLogin(String Number, String PIN) {
        Account account = new Account();
        try {
            TypedQuery<AccountEntity> query = entityManager.createQuery("SELECT a FROM AccountEntity a WHERE a.number = :number AND a.pin = :pin", AccountEntity.class);
            query.setParameter("number", Number);
            query.setParameter("pin", PIN);
            AccountEntity ae;
            ae = query.getSingleResult();
            if (ae != null) {
                AccountHelper helper = new AccountHelper();
                account = helper.toAccount(ae);

            }

        } catch (Exception e) {
             java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            return account;
        }
    }

    public List<Account> GetAllAccounts() {
        List<Account> list = new ArrayList<>();
        try {
            List<AccountEntity> findAccountEntityEntities = jpaController.findAccountEntityEntities();
            for (AccountEntity accountEntity : findAccountEntityEntities) {
                list.add(accountHelper.toAccount(accountEntity));
            }
        } catch (Exception e) {
             java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            return list;
        }
    }
        
    public Account UpdateAccount(Account acc) {
        try {
            AccountEntity entity = jpaController.findAccountEntity(acc.getId());
            entity.setPin(acc.getPin());
            entity.setStatus(acc.getStatus());
            jpaController.edit(entity);
            
           // entityManager.refresh(entity);
            jpaController.getEntityManager().close();
            return accountHelper.toAccount(entity);
           
        } catch (IllegalOrphanException ex) {
            java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
           java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
           java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        }
     return null;
    }
    
    
    public Account AddAccount(Account acc){
        try {
            AccountEntity entity = new AccountEntity();
            entity.setNumber(acc.getNumber());
            entity.setPin(acc.getPin());
            entity.setCustomerId(customerEntityJpaController.findCustomerEntity(acc.getCustomerId()));
            entity.setStatus(acc.getStatus());
            jpaController.create(entity);
          //  jpaController.getEntityManager().refresh(entity);
            return accountHelper.toAccount(entity);
            
           
        } catch (PreexistingEntityException ex) {
            java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally {
            return null;
        }
        
    }
    
    public boolean DeleteAccount(int id){
        try {
            jpaController.destroy(id);
            return true;
        } catch (IllegalOrphanException ex) {
            java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackFailureException ex) {
            java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
        
    }
}
